package org.openkoala.koala.popup.javaclass.crud;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.deploy.curd.module.pojo.ActionNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ApplicationNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ControllerNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ControllerWebFormpageNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ControllerWebListpageNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ControllerWebViewpageNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.DTONewFile;
import org.openkoala.koala.deploy.curd.module.pojo.ImplNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.NewFile;
import org.openkoala.koala.deploy.curd.module.pojo.WebFormpageNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.WebListpageNewFile;
import org.openkoala.koala.deploy.curd.module.pojo.WebViewpageNewFile;
import org.openkoala.koala.pojo.MavenProject;
import org.openkoala.koala.pojo.ModuleType;

public class GerenateFilesListDialog extends Dialog {

	private List<NewFile> files;
	private CrudAction action;

	private List<MavenProject> applicationModules = new ArrayList<MavenProject>();
	private List<MavenProject> applicationImplModules = new ArrayList<MavenProject>();
	private List<MavenProject> webModules = new ArrayList<MavenProject>();
	
	private Group applicationGroup;
	private Group applicationImplGroup;
	private Group webGroup;
	
	private Composite applicationRadioComposite;
	private Composite applicationImplRadioComposite;
	private Composite webRadioComposite;
	
	private Composite applicationFilesListComposite;
	private Composite applicationImplFilesListComposite;
	private Composite webFilesListComposite;
	
	private Color backGroundColor;
	private Color warnColor;
	private Color normalFontColor;
	
	protected GerenateFilesListDialog(Shell shell, List<NewFile> files, MavenProject project, CrudAction action) {
		super(shell);
		this.files = files;
		this.action = action;
		this.backGroundColor = shell.getDisplay().getSystemColor(SWT.COLOR_WHITE);
		this.warnColor = shell.getDisplay().getSystemColor(SWT.COLOR_RED);
		this.normalFontColor = shell.getDisplay().getSystemColor(SWT.COLOR_BLACK);
		
		if (project != null) {
			for (MavenProject module : project.getChilds()) {
	            if(module.getType() == ModuleType.Application) {
	            	applicationModules.add(module);
	            	continue;
	            }
	            
	            if(module.getType() == ModuleType.Impl) {
	            	applicationImplModules.add(module);
	            	continue;
	            }
	            
	            if(module.getType() == ModuleType.War) {
	            	webModules.add(module);
	            }
			}
		}
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Gerenate Files List");
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		initializeDialogUnits(composite);
		GridData mainGridData = new GridData(550, 430);

		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(mainGridData);
		composite.setBackground(backGroundColor);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		
		initGroups(composite);
		
		return parent;
	}

	private void initGroups(Composite composite) {
		initApplicationGroup(composite);
		initApplicationImplGroup(composite);
		initWebGroup(composite);
		
		initAppFilesListComposite();
		initAppImplFilesListComposite();
		initWebFilesListComposite();
		initFilesList();
		
		initAppRadioComposite();
		initAppImplRadioComposite();
		initWebRadioComposite();
	}
	
	private void initApplicationGroup(Composite composite) {
		GridData applicationGroupGridData = new GridData(GridData.FILL_HORIZONTAL);
		applicationGroupGridData.grabExcessHorizontalSpace = true;
		applicationGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
		applicationGroup.setText("Choose which application module to create these files.");
		applicationGroup.setLayout(new GridLayout(1, false));
		applicationGroup.setLayoutData(applicationGroupGridData);
	}
	
	private void initApplicationImplGroup(Composite composite) {
		GridData applicationImplGroupGridData = new GridData(GridData.FILL_HORIZONTAL);
		applicationImplGroupGridData.grabExcessHorizontalSpace = true;
		applicationImplGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
		applicationImplGroup.setText("Choose which application implement module to create these files.");
		applicationImplGroup.setLayout(new GridLayout(1, false));
		applicationImplGroup.setLayoutData(applicationImplGroupGridData);
	}
	
	private void initWebGroup(Composite composite) {
		GridData webGroupGridData = new GridData(GridData.FILL_HORIZONTAL);
		webGroupGridData.grabExcessHorizontalSpace = true;
		webGroup = new Group(composite, SWT.SHADOW_ETCHED_IN);
		webGroup.setText("Choose which web module to create these files.");
		webGroup.setLayout(new GridLayout(1, false));
		webGroup.setLayoutData(webGroupGridData);
	}
	
	private void initAppRadioComposite() {
		GridData appRadioCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		appRadioCompositeGridData.grabExcessHorizontalSpace = true;
		applicationRadioComposite = new Composite(applicationGroup, SWT.SHADOW_ETCHED_IN);
		applicationRadioComposite.setLayout(new GridLayout(3, false));
		applicationRadioComposite.setLayoutData(appRadioCompositeGridData);

		createRadio(applicationModules, applicationRadioComposite);
	}
	
	private void initAppImplRadioComposite() {
		GridData appImplRadioCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		appImplRadioCompositeGridData.grabExcessHorizontalSpace = true;
		applicationImplRadioComposite = new Composite(applicationImplGroup, SWT.SHADOW_ETCHED_IN);
		applicationImplRadioComposite.setLayout(new GridLayout(3, false));
		applicationImplRadioComposite.setLayoutData(appImplRadioCompositeGridData);

		createRadio(applicationImplModules, applicationImplRadioComposite);
	}
	
	private void initWebRadioComposite() {
		GridData webRadioCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		webRadioCompositeGridData.grabExcessHorizontalSpace = true;
		webRadioComposite = new Composite(webGroup, SWT.SHADOW_ETCHED_IN);
		webRadioComposite.setLayout(new GridLayout(3, false));
		webRadioComposite.setLayoutData(webRadioCompositeGridData);
		
		createRadio(webModules, webRadioComposite);
	}
	
	private void initAppFilesListComposite() {
		GridData appFLCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		appFLCompositeGridData.grabExcessHorizontalSpace = true;
		applicationFilesListComposite = new Composite(applicationGroup, SWT.SHADOW_ETCHED_IN);
//		applicationFilesListGroup.setText("Application Files List");
		applicationFilesListComposite.setLayout(new GridLayout(1, true));
		applicationFilesListComposite.setLayoutData(appFLCompositeGridData);
	}
	
	private void initAppImplFilesListComposite() {
		GridData appImplFLCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		appImplFLCompositeGridData.grabExcessHorizontalSpace = true;
		applicationImplFilesListComposite = new Composite(applicationImplGroup, SWT.SHADOW_ETCHED_IN);
//		applicationImplFilesListGroup.setText("Application Files List");
		applicationImplFilesListComposite.setLayout(new GridLayout(1, true));
		applicationImplFilesListComposite.setLayoutData(appImplFLCompositeGridData);
	}
	
	private void initWebFilesListComposite() {
		GridData webImplFLCompositeGridData = new GridData(GridData.FILL_HORIZONTAL);
		webImplFLCompositeGridData.grabExcessHorizontalSpace = true;
		webFilesListComposite = new Composite(webGroup, SWT.SHADOW_ETCHED_IN);
//		webFilesListGroup.setText("Application Files List");
		webFilesListComposite.setLayout(new GridLayout(1, true));
		webFilesListComposite.setLayoutData(webImplFLCompositeGridData);
	}
	
	private void createRadio(List<MavenProject> modules, Composite composite) {
		boolean hasSelectionRadio = false;
		for (MavenProject module : modules) {
			Button radio = new Button(composite, SWT.RADIO);
			radio.setText(module.getName());
			radio.setData(module);
			radio.addSelectionListener(new RadioSelectionListener());
			if (!hasSelectionRadio) {
				for (NewFile newFile : files) {
					if (newFile.getProjectPath().equals(module.getPath())) {
						radio.setSelection(true);
						break;
					}
				}
				hasSelectionRadio = true;
			}
		}
	}

	private void initFilesList() {
		for (NewFile newFile : files) {
			if (newFile instanceof DTONewFile || newFile instanceof ApplicationNewFile) {
				createFileText(applicationFilesListComposite, newFile);
				applicationGroup.layout();
				continue;
			}
			
			if (newFile instanceof ImplNewFile) {
				createFileText(applicationImplFilesListComposite, newFile);
				applicationImplGroup.layout();
				continue;
			}
			
			if (newFile instanceof ActionNewFile || newFile instanceof WebFormpageNewFile || newFile instanceof WebListpageNewFile || newFile instanceof WebViewpageNewFile
					|| newFile instanceof ControllerNewFile || newFile instanceof ControllerWebFormpageNewFile || newFile instanceof ControllerWebListpageNewFile || newFile instanceof ControllerWebViewpageNewFile) {
				createFileText(webFilesListComposite, newFile);
				webGroup.layout();
			}
		}
	}
	
	private void createFileText(Composite composite, NewFile newFile) {
		Text text = new Text(composite, SWT.SINGLE);
		text.setText(newFile.getPackagePath());
		text.setData(newFile);
		text.setEditable(false);
		
		if (newFile.isFileExists()) {
			text.setForeground(warnColor);
		}
	}

	@Override
	protected void okPressed() {
		action.generateCodeByCrudConfig(files);
		super.okPressed();
	}
	
	private class RadioSelectionListener implements SelectionListener {
		@Override
		public void widgetSelected(SelectionEvent event) {
			Button radio = (Button) event.getSource();
			MavenProject module = (MavenProject) radio.getData();
			
			if (module.getType() == ModuleType.Application) {
				checkWarn(applicationFilesListComposite, module.getPath());
				return;
			}
			if (module.getType() == ModuleType.Impl) {
				checkWarn(applicationImplFilesListComposite, module.getPath());
				return;
			}
			if (module.getType() == ModuleType.War) {
				checkWarn(webFilesListComposite, module.getPath());
			}
			
		}

		private void checkWarn(Composite fileListComposite, String projectPath) {
			for (Control child : fileListComposite.getChildren()) {
				Text fileNameText = (Text) child;
				NewFile newFile = (NewFile) fileNameText.getData();
				newFile.setProjectPath(projectPath);
				if (newFile.isFileExists()) {
					fileNameText.setForeground(warnColor);
				} else {
					fileNameText.setForeground(normalFontColor);
				}
			}
		}
		
		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}
		
	}
}
